//
//  ViewController.m
//  FM
//
//  Created by MB__Lee on 2018/4/9.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

#import "AFNetworking.h"


#import "OutModel.h"
#import "JSONModel.h"

#import <MJRefresh.h>

#import "FMTableview.h"



#define FB @"https://yq.qidongit.com/api/service/d350cb15-e471-402f-b1a1-a8bf21dbbfe3/projects?token=CfDJ8N6xZhFUEjpFpl4bBQl3hXhLYsM5Ep4iJ3eNZETkq8WTQj8uixp6g_MKyqKBf2IDYvMvUDYMMVzm6K0BZ__UfeZ4AlGCxNXr0oM-x4FWLTO56POkeaxel_BC-fCI3RXRE0pxbxB12W-z7ePPH7yeQ5DFiA8BfoyYB6dNtNNSMF5o"

@interface ViewController ()

@property(nonatomic,strong)AFHTTPSessionManager *manager;

@property(nonatomic,strong)NSMutableArray<OutModel *> *mut;//网络请求的解析数据

@property(nonatomic,strong)FMTableview *tableview;


@end

@implementation ViewController
-(AFHTTPSessionManager *)manager{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
    }
    return  _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    _tableview = [[FMTableview alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //添加tableviewHeader-----这个位置添加HeaderView
    _tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.view.bounds.size.width, 200)];
    _tableview.tableHeaderView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:_tableview];
    // Do any additional setup after loading the view, typically from a nib.
 
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    
        [self af];
        [_tableview.mj_header endRefreshing];
    }];
    
    
}
-(void)af{
        [_tableview.mj_header beginRefreshing];
    [self.manager GET:FB parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
       
        NSLog(@"%@",_mut);
        _mut = [NSMutableArray array];
        for (NSDictionary *dict in responseObject[@"data"]) {
            OutModel *model = [[OutModel alloc] initWithDictionary:dict error:nil];
            [model caculateCellNumAndCellHeight];
            [_mut addObject:model];
        }
        
        
        
        _tableview.mutData = _mut;
        
        
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString *path  =[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
        for (NSDictionary *dicty in dict[@"data"]) {
            OutModel *model = [[OutModel alloc] initWithDictionary:dicty error:nil];
            [model caculateCellNumAndCellHeight];
            [_mut addObject:model];
        }
        _tableview.mutData = _mut;
        
    }];
}
-(NSString *)getFilePath{
    return  @"/Users/mb__lee/Desktop/test.plist";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
